package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.wechat.pay.java.service.payments.model.Transaction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private PaymentInfoService paymentInfoService;

    /**
     * 退款
     * @param orderNo
     * @return
     */
    @Operation(summary = "退款")
    @GetMapping("/refund/{orderNo}")
    public Result refund(@PathVariable String orderNo){
        //  调用服务层方法.
        boolean flag = this.wxPayService.refund(orderNo);
        //  返回结果
        return Result.ok(flag);
    }


    /**
     * 关闭交易记录
     * @param orderNo
     * @return
     */
    @Operation(summary = "关闭交易记录")
    @GetMapping("/closePayment/{orderNo}")
    public Result closePayment(@PathVariable String orderNo){
        //  调用服务层方法.
        boolean flag = this.paymentInfoService.closePayment(orderNo);
        //  返回结果
        return Result.ok(flag);
    }

    //  https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_5.shtml
    /**
     * 异步回调
     * @param request
     * @return
     */
    @Operation(summary = "微信支付异步通知接口")
    @PostMapping("/notify")
    public Result wxNotify(HttpServletRequest request){
        //  创建map
        Map<String,Object> result = new HashMap<>();
        //  调用服务层方法.
        Transaction transaction = wxPayService.wxNotify(request);
        //  判断
        if (null != transaction && transaction.getTradeState().equals(Transaction.TradeStateEnum.SUCCESS)){
            //  调用修改交易记录方法.
            paymentInfoService.updatePaymentStatus(transaction);
            //  支付成功.
            result.put("code","SUCCESS");
            result.put("message","成功");
        } else {
            result.put("code","FAIL");
            result.put("message","失败");
        }
        //  返回数据类型
        return Result.ok(result);
    }
    /**
     * 查询支付状态 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_2.shtml
     * @param orderNo
     * @return
     */
    @Operation(summary = "支付状态查询")
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo){
        try {
            //  调用服务层方法
            wxPayService.queryPayStatus(orderNo);
        } catch (Exception e) {
            return Result.fail();
        }
        //  返回数据
        return Result.ok();
    }

    /**
     * 微信支付
     * @param paymentType   订单1301 充值1302
     * @param orderNo       订单或充值单号
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "微信下单")
    @PostMapping("/createJsapi/{paymentType}/{orderNo}")
    public Result createJsapi(@PathVariable String paymentType,
                              @PathVariable String orderNo){
        //  调用服务层方法.
        Map<String,Object> result = wxPayService.createJsapi(paymentType,orderNo);
        /*
        Map<String, Object> result = new HashMap();
        result.put("timeStamp", response.getTimeStamp()); // 时间戳
        result.put("nonceStr", response.getNonceStr());   // 随机字符串
        result.put("package", response.getPackageVal());  // 订单详情扩展字符串
        result.put("signType", response.getSignType());   // 签名方式
        result.put("paySign", response.getPaySign());     // 签名
         */
        //  返回数据.
        return Result.ok(result);
    }

    /**
     *
     * @param orderNo
     * @return
     */
    @GetMapping("closePay/{orderNo}")
    public Result closePay(@PathVariable String orderNo){
        boolean result = this.wxPayService.closePay(orderNo);
        return Result.ok(result);
    }


}
